package com.itheima.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.SmsHomeNewProductMapper;
import com.itheima.pojo.SmsHomeBrand;
import com.itheima.pojo.SmsHomeNewProduct;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* @author lenovo
* @description 针对表【sms_home_new_product(新鲜好物表)】的数据库操作Service实现
* @createDate 2022-10-11 14:26:18
*/
@DubboService
public class SmsHomeNewProductApiImpl implements SmsHomeNewProductApi {
    @Autowired
    private SmsHomeNewProductMapper newProductMapper;

    /**
     * 添加首页新品
     * @param newProduct
     */
    public void save(SmsHomeNewProduct newProduct) {
        newProductMapper.insert(newProduct);
    }

    /**
     * 分页查询新品
     * @param productName
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @return
     */
    public PageResult findNewProduct(String productName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        //构造查询条件
        //分页构造器
        IPage<SmsHomeNewProduct> iPage = new Page<>(pageNum, pageSize);
        //条件构造器
        LambdaQueryWrapper<SmsHomeNewProduct> lqw = new LambdaQueryWrapper<>();
        //条件判断
        lqw.like(StrUtil.isNotBlank(productName), SmsHomeNewProduct::getProductName, productName)
                .eq(recommendStatus!=null,SmsHomeNewProduct::getRecommendStatus, recommendStatus)
                .orderByDesc(SmsHomeNewProduct::getSort);

        //System.out.println(advertise.getEndTime());
        newProductMapper.selectPage(iPage, lqw);
        List<SmsHomeNewProduct> list = iPage.getRecords();
        Integer total = Convert.toInt(iPage.getTotal());//总条数
        Integer totalPage = Convert.toInt(iPage.getPages());//当前分页总页数
        return new PageResult(pageNum, pageSize, total, totalPage, list);
    }

    /**
     * 根据id查询新平
     * @param id
     * @return
     */
    public SmsHomeNewProduct findBrandById(Long id) {
        return newProductMapper.selectById(id);
    }

    /**
     * 修改新品排序
     * @param newProduct
     */
    public void updateSortByid(SmsHomeNewProduct newProduct) {

        newProductMapper.updateById(newProduct);
    }

    /**
     * 批量删除新品
     * @param ids
     * @return
     */
    public boolean deleteNewProduct(List<Long> ids) {
        //条件构造器
        LambdaQueryWrapper<SmsHomeNewProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(SmsHomeNewProduct::getId,ids);
        int delete = newProductMapper.delete(lqw);
        return delete>0;
    }

    @Override
    public boolean updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        //条件构造器
        LambdaQueryWrapper<SmsHomeNewProduct> lqw = new LambdaQueryWrapper<>();
        lqw.in(SmsHomeNewProduct::getId,ids);
        SmsHomeNewProduct newProduct = new SmsHomeNewProduct();
        newProduct.setRecommendStatus(recommendStatus);
        int count = newProductMapper.update(newProduct,lqw);
        return count>0;
    }
}




